#!/usr/bin/python
# -*- coding: utf-8 -*-

# ESiPC
from esipc import ESiPCProperty

from mikan.viewlet import *

# ESiPC
# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class AbstractMikanBaseFormViewlet(MikanDivViewlet):
	def get_form_action(self):
		return self.FFormAction

	def is_top_buttons(self):
		return self.FTopButtons

	def set_top_buttons(self, value):
		self.FTopButtons = value

	def is_bottom_buttons(self):
		return self.FBottomButtons

	def set_bottom_buttons(self, value):
		self.FBottomButtons = value

	def __init__(self, parent, action=None, name=None, method="post", target=None):
		super(AbstractMikanBaseFormViewlet, self).__init__(parent)

		self.clazz = "item"
		self.FTopButtons = True
		self.FBottomButtons = True

		self.FFormName = name
		if action != None:
			self.FFormAction = action
		else:
			try:
				s = self.request.pathinfo.split("/")
				self.FFormAction = s[len(s) - 1]
			except:
				self.FFormAction = self.request.scriptname + self.request.pathinfo

	def add_buttons(self, parent):
		buttons = MikanDivViewlet(parent)
		buttons.clazz = "buttons"

		return buttons

	def add_button(self, buttons, title, url=None, click=None, id=None, clazz="button"):
		span = MikanSpanViewlet(buttons)
		a = MikanAViewlet(span, href=None, nodevalue=title)
		if url:
			a.href = url
		a.onclick = click
		a.clazz = clazz
		if id:
			a.id = id

		return span

	def add_content(self, table):
		# フォーム本体
		return MikanTbodyViewlet(table)

	def build_content(self):
		# エラー表示
		MikanExceptionsViewlet(self)

		# フォーム
		f = self.formaction
		if self.islogdebug:
			self.logger.debug(f)
		form = MikanFormViewlet(self, action=f, name="item")

		# ボタン
		if self.istopbuttons:
			self.add_buttons(form)

		# テーブル
		table = MikanTableViewlet(form)
		table.clazz = "item_list"
		table.id = "item_list"

		# フォーム本体
		self.add_content(table)

		# ボタン
		if self.isbottombuttons:
			self.add_buttons(form)

	# ----------------------------------------------------
	## 処理
	# @return Trueなら続行
	def perform(self):
		# コンテンツ構築
		self.build_content()

		# 継承元実行
		if not super(AbstractMikanBaseFormViewlet, self).perform():
			return False

		# 実行結果
		return True

	def get_form_name(self):
		return self.FFormName

	# ----------------------------------------------------
	## Property
	formaction = property(get_form_action, None, None)
	form_name = ESiPCProperty(get_form_name, None, None)
	istopbuttons = property(is_top_buttons, set_top_buttons, None)
	isbottombuttons = property(is_bottom_buttons, set_bottom_buttons, None)

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanBaseFormViewlet(AbstractMikanBaseFormViewlet):

	def __init__(self, parent, action=None, name=None, method="post", target=None):
		super(MikanBaseFormViewlet, self).__init__(parent, action, name, method, target)
		self.FOnContent = None

	def get_submit_caption(self):
		return u"実行"

	def get_reset_caption(self):
		return u"クリア"

	def get_submit_code(self):
		return "mikan.form.item_submit();"

	def get_reset_code(self):
		return "mikan.form.item_reset();"

	def add_buttons(self, parent):
		buttons = super(MikanBaseFormViewlet, self).add_buttons(parent)

		self.add_button(buttons, self.submitcaption, None, self.submitcode)
		self.add_button(buttons, self.resetcaption, None, self.resetcode)

		return buttons

	def add_content(self, table):
		tbody = super(MikanBaseFormViewlet, self).add_content(table)

		if self.oncontent != None:
			self.oncontent(table, tbody)

	def set_oncontent(self, value):
		self.FOnContent = value

	def get_oncontent(self):
		return self.FOnContent

	# ----------------------------------------------------
	## Property
	oncontent = ESiPCProperty(get_oncontent, set_oncontent, None)
	submitcaption = ESiPCProperty(get_submit_caption, None, None)
	resetcaption = ESiPCProperty(get_reset_caption, None, None)
	submitcode = ESiPCProperty(get_submit_code, None, None)
	resetcode = ESiPCProperty(get_reset_code, None, None)
